﻿using Ewelve_Mouth_HD.Model;
using System;

namespace Ewelve_Mouth_HD.DAL
{
    public class NubDal
    {
        private readonly Mycontext context;

        public NubDal(Mycontext _context)
        {
            context = _context;
        }
        /// <summary>
        /// 房号添加
        /// </summary>
        /// <param name="E"></param>
        /// <returns></returns>
        public int NubAdd(RoomNub E)
        {
            try
            {
                var G = context.RoomNubs.Count(X => X.NubCard == E.NubCard);
                if (G > 0)
                {
                    return -1;
                }
                context.RoomNubs.Add(E);
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="NubCard"></param>
        /// <param name="TypeName"></param>
        /// <param name="StartDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public IQueryable RoomNubShow(string? NubCard, int TypeName, DateTime? StartDate, DateTime? EndDate)
        {
            var Q = context.RoomNubs.Join(context.RoomTypes, A => A.TypeName, B => B.RoomId, (A, B) => new 
            {
                A.NubId,
                A.TypeName,
                A.NubCard,
                A.NubSay,
                A.NubStage,
                A.CreateDate,
                A.IsDelete,
                B.RoomName
            }).Where(T=>T.IsDelete==false);
            if (!string.IsNullOrEmpty(NubCard))
            {
                Q = Q.Where(T => T.NubCard == NubCard);
            }
            if (TypeName != 0)
            {
                Q=Q.Where(T => T.TypeName == TypeName);
            }
            if(StartDate != null)
            {
                Q = Q.Where(T => T.CreateDate >= StartDate);
            }
            if (EndDate != null)
            {
                Q = Q.Where(T => T.CreateDate >= EndDate);
            }
            if (StartDate != null && EndDate != null)
            {
                Q = Q.Where(T => T.CreateDate >= StartDate&& T.CreateDate >= EndDate);
            }
            return Q;
        }
        /// <summary>
        /// 查询单个数据
        /// </summary>
        /// <param name="NubId"></param>
        /// <returns></returns>
        public RoomNub FirstOnly(int NubId)
        {
            try
            {
                return context.RoomNubs.Find(NubId);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="NubId"></param>
        /// <returns></returns>
        public int DropNup(int NubId)
        {
            try
            {
                var H = FirstOnly(NubId);
                if (H != null)
                {
                    H.IsDelete= true;
                }
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="NubId"></param>
        /// <param name="NubStage"></param>
        /// <returns></returns>
        public int EditStage(int NubId, int NubStage)
        {
            try
            {
                var H = FirstOnly(NubId);
                if (H != null)
                {
                    H.NubStage = NubStage;
                }
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Y"></param>
        /// <returns></returns>
        public int EditNub(RoomNub Y)
        {
            try
            {
                var H = FirstOnly(Y.NubId);
                if (H != null)
                {
                    H.TypeName = Y.TypeName;
                    H.NubSay=Y.NubSay;
                    H.NubStage=Y.NubStage;
                }
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int SumDrop(string ids)
        {
            try
            {
                string[] G = ids.Split(",");
                foreach (string Z in G)
                {
                    var T = FirstOnly(Convert.ToInt32(Z));
                    T.IsDelete = true;
                }
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="NubStage"></param>
        /// <returns></returns>
        public int SumEditSatage(string ids, int NubStage)
        {
            try
            {
                string[] G = ids.Split(",");
                foreach (string Z in G)
                {
                    var T = FirstOnly(Convert.ToInt32(Z));
                    T.NubStage = NubStage;
                }
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 绑定下拉框
        /// </summary>
        /// <returns></returns>
        public IQueryable NubShow()
        {
            try
            {
                return context.RoomNubs.Where(Q => Q.IsDelete == false);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
